package com.xf;

import com.xf.common.Invocation;
import com.xf.common.Serviceinfo;
import com.xf.protocol.HttpClient;

import java.io.IOException;
import java.util.Scanner;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Consumer {
    public static void main(String[] args) throws Exception {
        if (args.length != 4) {
            System.out.println("使用示例: java -jar ConfigApp.jar -r <rigister> -p <port> ");
            return;
        }
        String RigistAddress=null;//注册中心ip地址
        Integer port=null;//注册中心端口
        for (int i = 0; i < args.length; i++) {
            switch (args[i]) {
                case "-r":
                    if (i + 1 < args.length) {
                        RigistAddress = args[++i];
                    } else {
                        System.out.println("错误: 没有提供注册中心ip地址");
                        return;
                    }
                    break;
                case "-p":
                    if (i + 1 < args.length) {
                        try {
                            port = Integer.parseInt(args[++i]);
                        } catch (NumberFormatException e) {
                            System.out.println("错误: 端口号必须是整数");
                            return;
                        }
                    } else {
                        System.out.println("错误: 没有提供端口号");
                        return;
                    }
                    break;
                default:
                    System.out.println("未知命令: " + args[i]);
                    return;
            }
        }
        while (true){
            System.out.println("请选择操作:");
            System.out.println("1.刷新服务列表");
            System.out.println("2.调用服务");
            System.out.println("3.启动编写的程序（可自行编辑）");
            Scanner scanner = new Scanner(System.in);
            int x=scanner.nextInt();
            Serviceinfo service=null;
            if(x==1){
                //客户端申请查看有哪些可调用服务
                System.out.println("向注册中心请求服务列表中。。。");
                //Thread.sleep(2000);
                HttpClient httpClient1 = new HttpClient();
                service = httpClient1.req_for_service(RigistAddress, port);
                service.print();
            }
            if(x==2){
                //调用服务
                System.out.println("请输入请求服务的名称");
                String intefacename = scanner.next();
                System.out.println("请输入请求服务的版本");
                String version=scanner.next();


                String[] part1=null;
                String methodname=null;
                while (true){
                    System.out.println("请输入请求的方法和参数类型,例:sayingHello(String,String)");
                    String string1 = scanner.next();
                    // 定义正则表达式，匹配括号和逗号
                    String regex = "[(),]";
                    // 创建Pattern对象
                    Pattern pattern = Pattern.compile(regex);
                    // 使用正则表达式分割字符串
                    part1 = pattern.split(string1);
                    methodname=part1[0];
                    if (part1.length>1) break;
                    else System.out.println("输入格式有误！");
                }

                System.out.println("请输入参数列表,例:xf 666");scanner.nextLine();
                String string2=scanner.nextLine()+" ";
                String[] part2 = string2.split(" ");
                while(part2.length!=part1.length-1){
                    System.out.println("参数数量有误！"+part1.length+part2.length);
                    System.out.println("请输入参数列表,例:xf");
                    string2=scanner.nextLine()+" ";
                    part2 = string2.split(" ");
                }

                //参数转换
                Class[] parameterTypes = new Class[part1.length-1];
                Object[] parameters = new Object[part2.length];
                try{
                    for (int i = 0; i < part1.length-1; i++){
                        //System.out.println(part1[i+1]);
                        parameterTypes[i]=Class.forName("java.lang."+part1[i+1]);
                        parameters[i] = convertToType(part2[i], parameterTypes[i]);
                        //System.out.println(parameters[i]);
                    }
                }
                catch (Exception e){
                    System.out.println("参数转换时出错，请检查你输入的函数和参数是否标准！");
                    continue;
                }

                Invocation invocation = new Invocation(intefacename, methodname, parameterTypes, parameters, version);
                HttpClient httpClient2=new HttpClient();

                String result = httpClient2.cfs_con_rig(RigistAddress, port, invocation).toString();
                System.out.println("返回的结果为: "+result);
            }
            if(x==3){
                Program.prepare(RigistAddress,port);
                Program.start();
            }
        }
    }

    private static Object convertToType(String value, Class<?> type) {
        if (type == Integer.class || type == int.class) {
            return Integer.parseInt(value);
        } else if (type == Double.class || type == double.class) {
            return Double.parseDouble(value);
        } else if (type == Boolean.class || type == boolean.class) {
            return Boolean.parseBoolean(value);
        } else if (type == String.class) {
            return value;
        } else if ((type == char.class || type==Character.class) && value.length()==1) {
            return value.charAt(0);
        } else {
            throw new IllegalArgumentException("Unsupported type: " + type.getName());
        }
    }
}
